#!/bin/sh
# simple configuration script 
# @author UENO Katsuhiro
# @author YAMATODANI Kiyoshi

projname=mllex
depends=

prefix=/usr/local
exec_prefix='$(prefix)'
compiler=mlton

for opt in "$@"
do
  optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
  case "$opt" in
    --prefix=*) prefix="$optarg" ;;
    --exec_prefix=*) exec_prefix="$optarg" ;;
    --with-smlnj) compiler=smlnj ;;
    --with-mlton) compiler=mlton ;;
    --with-smlsharp) compiler=smlsharp ;;
    --help)
      echo "Usage: $0 OPTIONS..."
      echo
      echo "Options:"
      echo "  --help                print this message."
      echo "--prefix=DIR          install files to DIR [default=$prefix]"
      echo "--exec-prefix=DIR     isntall executables to DIR [default=$prefix]"
      echo "--with-smlnj          use SML/NJ [default=no]"
      echo "--with-mlton          use MLton [default=yes]"
      echo "--with-smlsharp       use SML# [default=no]"
      ;;
    *)
      echo "$0: error: unrecognized option: $opt" 1>&2
      echo "Try \`$0 --help' for more information." 1>&2
      exit 1
      ;;
  esac
done

for i in $depends; do
  module=`dirname $i`
  if [ -f "../$i" ]; then result=yes; else result=no; fi
  echo "checking for dependency on $module... $result"
  if [ "x$result" = "xno" ]; then
    echo "** Build \`$module' by the following command before building this module:"
    echo "   cd ../$module && ./configure && make"
    exit 1
  fi
done

echo "SML compiler for building $projname... $compiler"

cat > commonrule <<EOF
# This file is automatically generated by configure script.
prefix = $prefix
exec_prefix = $exec_prefix
bindir = \$(exec_prefix)/bin
libdir = \$(prefix)/lib
libdir_$projname = \$(libdir)/$projname
heapdir = \$(libdir_$projname)/heap
SHELL = /bin/sh
MLYACC = \$(srcdir)/../ml-yacc/smlyacc
MLLEX = \$(srcdir)/../ml-lex/smllex
SMLDOC = \$(srcdir)/../smldoc/smldoc
INSTALL = install
INSTALL_PROGRAM = \$(INSTALL)
INSTALL_DATA = \$(INSTALL)
SML_COMPILER = $compiler
MAKESML = \
 COMPILER='\$(SML_COMPILER)' \
 INSTALL='\$(INSTALL)' \
 BINDIR='\$(DESTDIR)\$(bindir)' \
 HEAPDIR='\$(DESTDIR)\$(heapdir)' \
 \$(SHELL) \$(srcdir)/../SMLSharp/makesml
# commonrule ends here.
EOF

for i in \
  Makefile
do
  echo "creating $i"
  sed "
s|@srcdir_$projname@|.|
s|@builddir@|.|
s|@top_srcdir@|.|
s|@top_builddir@|.|
  " "$i.in" > "$i"
done

# configure ends here.
